<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task extends CI_Controller {


	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('task_mod','tk');
		$this->load->model('chat_mod','ct');
		$this->load->model('user_mod','user');
		$this->load->model('task_name','tn');
		$this->load->model('Search_mod','search');
		//$this->load->library('json');
		$this->load->library('session');
	}
	
	//客户任务
	public function gettotask_ut($ut_id=''){
		$this->checklogining();
		$uid=$this->session->userdata('u_id');
		$ut_id=$this->session->userdata('ut_id');
		$data['username']=$this->session->userdata('username');
		$data['task_name'] = $this->tn->get_task_name();
		$dutyobj=$this->tk->getduty($uid);
		$data['duty']=nl2br($dutyobj->duty);
		$data['uid']=$uid;
		$data['ut_id']=$ut_id;
		$userlist = $this->search->getuserlist();
		foreach($userlist as $key=>$val){
			$ct = $this->tk->count_task($val['m_id']);
			$ctk = $this->tk->gettotask_count($val['m_id']);
			$userlist[$key]['passtime'] = $ct['c'];
			$userlist[$key]['no'] = $ctk['c'];
		}
		$user_list=$this->tk->getalluser(1);
		foreach($user_list as $key=>$val){
			$ctk = $this->tk->gettotask_count($val['m_id']);
			$user_list[$key]['no'] = $ctk['c'];
		}
		$data["user_list"]=$user_list;
		$data['userlist'] = $userlist;
		$this->tk->updatetotask();
		$tasktolist=array();
		$taskurllist=$this->tk->gettourl($uid);
		$tasktolist_g_ut=$this->tk->gettotask_g_ut($ut_id);
		$count = $this->tk->count_task($uid);
		$data['rank'] = $this->session->userdata('rank');
		foreach($tasktolist_g_ut as $keyr=>$val){
			$tasktolist[$val['completetime']]=$this->tk->gettotask_ut($ut_id,$val['completetime']);
			foreach($tasktolist[$val['completetime']] as $key=>$task){
				$desc_list[$val['completetime']]=$this->tk->getall_desc($task['t_id']);
				//
				$str='';
				$i=1;
				foreach($desc_list[$val['completetime']] as $desc)
				{
					$str.=$i.".".$desc['d_content']."<br>";
					$i++;
				}
				$tasktolist[$val['completetime']][$key]['desc']=$str;
			}
		}
		foreach($tasktolist as $key => $val){
			foreach($val as $k => $task){
				$task_arr = $this->tn->get_task_name_title($task['ut_id']);
				if(!empty($task_arr)){
					$tasktolist[$key][$k]['task_title'] = $task_arr[0]['task_title'];
				}else{
					$tasktolist[$key][$k]['task_title'] = '';
				}
			}
		}
		
		$data["task_count"]=$count['c'];
		$data["task_url"]=$taskurllist;
		$data["task_list"]=$tasktolist;
		$this->load->view('taskindex',$data);
			
	}
	
	//今日任务
	public function gettotask($ut_id='')
	{	
		$this->checklogining();
		$uid=$this->session->userdata('u_id');
		$data['username']=$this->session->userdata('username');
		$data['task_name'] = $this->tn->get_task_name();
		$dutyobj=$this->tk->getduty($uid);
		$data['duty']=nl2br($dutyobj->duty);
		$data['uid']=$uid;
		$data['ut_id']=0;
		$userlist = $this->search->getuserlist();
		foreach($userlist as $key=>$val){
			$ct = $this->tk->count_task($val['m_id']);
			$ctk = $this->tk->gettotask_count($val['m_id']);
			$userlist[$key]['passtime'] = $ct['c'];
			$userlist[$key]['no'] = $ctk['c'];
		}
		$data['userlist'] = $userlist;
		$this->tk->updatetotask();
		$tasktolist=array();
		if($data['rank'] = $this->session->userdata('rank')>0)
		{
			$user_list=$this->tk->getalluser();
			foreach($user_list as $key=>$val){
				$ctk = $this->tk->gettotask_count($val['m_id']);
				$user_list[$key]['no'] = $ctk['c'];
			}
			$data["user_list"]=$user_list;
			$taskurllist=$this->tk->gettourl();
			$tasktolist_g=$this->tk->getalltask_g();
			$count = $this->tk->count_task();
			
			foreach($tasktolist_g as $keyr=>$val){
				$tasktolist[$val['completetime']]=$this->tk->getalltask($val['completetime'],$ut_id);
				foreach($tasktolist[$val['completetime']] as $key=>$task)
				{
					$desc_list[$val['completetime']]=$this->tk->getall_desc($task['t_id']);
					$str='';
					$i=1;
					foreach($desc_list[$val['completetime']] as $desc)
					{
						$str.=$i.".".$desc['d_content']."<br>";
						$i++;
					}
					$tasktolist[$val['completetime']][$key]['desc']=$str;
				}
			}
		}else{
			$taskurllist=$this->tk->gettourl($uid);
			$tasktolist_g=$this->tk->gettotask_g($uid);
			$count = $this->tk->count_task($uid);
			foreach($tasktolist_g as $keyr=>$val){
				$tasktolist[$val['completetime']]=$this->tk->gettotask($uid,$val['completetime'],$ut_id);
				foreach($tasktolist[$val['completetime']] as $key=>$task){
					$desc_list[$val['completetime']]=$this->tk->getall_desc($task['t_id']);
					$str='';
					$i=1;
					foreach($desc_list[$val['completetime']] as $desc)
					{
						$str.=$i.".".$desc['d_content']."<br>";
						$i++;
					}
					$tasktolist[$val['completetime']][$key]['desc']=$str;
				}
			}		
		}
		
		foreach($taskurllist as $key => $val){
			$taskurllist[$key]['t_name'] = $this->autolink($val['t_name']);
		}
		
		foreach($tasktolist as $key => $val){
			foreach($val as $k => $task){
				$task_arr = $this->tn->get_task_name_title($task['ut_id']);
				@$tasktolist[$key][$k]['task_title'] = $task_arr[0]['task_title'];
			}
		}

		$uid=$this->session->userdata('u_id');
		$data["task_count"]=$count['c'];
		$data["task_url"]=$taskurllist;
		$data["task_list"]=$tasktolist;
		$this->load->view('taskindex',$data);
	}
	
	
	//聊天
	public function chat(){
		$this->checklogining();
		$uid=$this->session->userdata('u_id');
		$data['chat'] = $this->ct->chat($uid);
		$data['user'] = $this->user->get_user();
		$this->load->view('chat',$data);
	}
	public function comet(){
		$uid=$this->session->userdata('u_id');
		$filename  = dirname(__FILE__).'/data.txt';
		$filename1  = dirname(__FILE__).'/data1.txt';
		$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
		echo $user = isset($_GET['user']) ? $_GET['user'] : '';
		$user1 = '';
		$user2 = '';
		if(!empty($user)){
			$user = explode(',',$user);
			$user1 = $user[0];
			$user2 = $user[1];
		}
		if(!empty($user1)){
			$this->ct->add_msg($uid,$user1,$msg,$user2);//插入数据库
		}
		//先清空
		file_put_contents($filename,'');
		file_put_contents($filename1,'');
		//写入内容至文件
		if ($msg != '' || $user1!='')
		{
			file_put_contents($filename,$msg);
			file_put_contents($filename1,$user2);
			die();
		}
		
		
		set_time_limit(0);
		$lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
		
		//取得文件最后修改时间
		$currentmodif = filemtime($filename);
		
		while ($currentmodif <= $lastmodif) 
		{	
			//有释放CPU占用率的作用
			usleep(10000); 
			//清除文件缓存信息
			clearstatcache();
			$currentmodif = filemtime($filename);
			$currentmodif1 = filemtime($filename1);
			
		}
		// return a json array
		$response = array();
		$response['msg']     = file_get_contents($filename);
		$response['user']    = file_get_contents($filename1);//file_get_contents($filename);

		$response['timestamp'] = $currentmodif;
		
		//结束
		echo json_encode($response);
		ob_flush();
		flush();
	}
	//插入聊天记录
	public function addmsg(){
		$uid=$this->session->userdata('u_id');
		$username=$this->session->userdata('username');
		
		$msg_user=$this->input->post('msg_user');
		$msg=$this->input->post('msg');
		$this->ct->add_msg($uid,$msg_user,$msg,$username);
		
	}
	//判断是否链接
	public function autolink($foo=''){
			$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow>\1</a>', $foo);
		if( strpos($foo, "http") === FALSE ){
			$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\1" target=_blank rel=nofollow >\1</a>', $foo);
		}else{
			$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\1<a href="http://\2" target=_blank rel=nofollow >\2</a>', $foo);
		}
		return $foo;
	}
	
	 //ajax增加任务
	public function addtask()
	{
		$contents=$this->input->post('content');
		$t_rank=$this->input->post('t_rank');
		$completetime=$this->input->post('completetime');
		$task_name=$this->input->post('task_name');
		$str='';
		$contents=addslashes($contents);
		$user_id = $this->input->post('user_id');
		if($this->session->userdata('rank')>0)
		{	
			if(!$user_id)$user_id = $this->session->userdata('u_id');
			$this->tk->addtask($user_id,$contents,$t_rank,$completetime,$task_name);
			$tasktolist_g=$this->tk->getalltask_g();
			$count = $this->tk->count_task();
			foreach($tasktolist_g as $keyr=>$val){
				$taskalllist[$val['completetime']]=$this->tk->getalltask($val['completetime']);
				switch($val['completetime']){
					case 1:$str.= '今天';break;
					case 2:$str.= '明天';break;
					case 3:$str.= '后天';break;
					case 4:$str.= '过期('.$count['c'].')';break;
				}
				foreach($taskalllist[$val['completetime']] as $task)
				{	
					$task_arr = $this->tn->get_task_name_title($task['ut_id']);
					$task_title = '';
					if(!empty($task_arr)){
						$task_title = $task_arr[0]["task_title"];
					}
					$desc_list[$val['completetime']]=$this->tk->getall_desc($task['t_id']);
					$str_desc='';
					$i=1;
					foreach($desc_list[$val['completetime']] as $desc)
					{
						$str_desc.=$i.".".$desc['d_content']."<br>";
						$i++;
					}
					$str.="<li id=\"task_".$task['t_id']."\" class=\"hover_container menu_container\" title='".$task['t_rank']."'> 
						  <div id=\"show_task_13349446\" class=\"task show_task\" style=\"\"> 
							<div id=\"content_task_13349446 \"> 
							  <div id=\"balloon_container_task_13349446\" class=\"reset-on-dismissal\" style=\"display:none\"><div class=\"menu_content\"></div></div> 
							  <div class=\"checkbox hover_target\" > ";
					  if($task['status']==1)
					  {
						$str.="<input id=\"task_done\" name=\"task[done]\" onclick=\"changestatus(this,".$task['t_id'].")\" type=\"checkbox\" value=\"1\"  checked=\"checked\"/> ";
					  
					  }else{
						$str.="<input id=\"task_done\" name=\"task[done]\" onclick=\"changestatus(this,".$task['t_id'].")\" type=\"checkbox\" value=\"1\"  /> ";
					  }
					
					$str.="  </div> 
						  <div class=\"body\"> 
							<span  id='_".$task['t_id']."' class=\"hover_target\"> ";
						switch($task['t_rank'])
						{
							case 5:
								$str.="[<font>每日</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 4:
								$str.="[<font>BUG</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 3:
								$str.="[<font color=red>紧急</font>]<font color=red>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 2:
								$str.="[<font color=#CC6633>比较</font>]"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name'];break;
							case 1:
								$str.="<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name'];break;
							default :
								$str.="[<font>常用网址</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
						}
						
					 $str.="  [<font color=\"#FF6600\" size=\"-1\">".$task['m_username']."</font>]  <font color=\"#999999\" size=\"-1\">(".$task['completedate'].")</font><span class=\"appendTask\" style=\"color:#999999; font-size:12px; margin-left:10px\"></span>
							</span> 
							<span id=\"\" class=\"FR folder pointer\"><img src=\"".base_url()."images/022.png\" /></span>
						  </div> 
						  <div class=\"clear\"></div>
						</div>
						<div class=\"task_desc_list\" style=\"display:none;padding-left:30px;\">".$str_desc."</div>
						<div  class=\"task_c\"  style=\"height:0px;\" ><textarea style=\"display:none;\" id=\"task_text_".$task['t_id']."\" name=\"".$task['t_id']."\" ></textarea></div>
					  </div> 
					</li>";
				}
			}
		}else{
			$uid=$this->session->userdata('u_id');
			$ut_id=$this->session->userdata('ut_id');
			$this->tk->addtask($uid,$contents,$t_rank,$completetime,$task_name,$uid);
			$tasktolist_g=$this->tk->gettotask_g($uid);
			$count = $this->tk->count_task($uid);
			foreach($tasktolist_g as $keyr=>$val){
				$tasktolist[$val['completetime']]=$this->tk->gettotask($uid,$val['completetime'],$ut_id);
				switch($val['completetime']){
					case 1:$str.= '今天';break;
					case 2:$str.= '明天';break;
					case 3:$str.= '后天';break;
					case 4:$str.= '过期('.$count['c'].')';break;
					
				}
				
				foreach($tasktolist[$val['completetime']] as $k=>$task)
				{	
					$task_arr = $this->tn->get_task_name_title($task['ut_id']);
					$task_title ='';
					if(!empty($task_arr)){
						$task_title = $task_arr[0]["task_title"];
					}
					$desc_list[$val['completetime']]=$this->tk->getall_desc($task['t_id']);
					$str_desc='';
					$i=1;
					foreach($desc_list[$val['completetime']] as $desc)
					{
						$str_desc.=$i.".".$desc['d_content']."<br>";
						$i++;
					}
					$str.="<li id=\"task_".$task['t_id']."\" class=\"hover_container menu_container\" title='".$task['t_rank']."'>
						  <div id=\"show_task_13349446\" class=\"task show_task\" style=\"\"> 
							<div id=\"content_task_13349446 \"> 
							  <div id=\"balloon_container_task_13349446\" class=\"reset-on-dismissal\" style=\"display:none\"><div class=\"menu_content\"></div></div> 
							  <div class=\"checkbox hover_target\" > ";
					  if($task['status']==1)
					  {
						$str.="<input id=\"task_done\" name=\"task[done]\" onclick=\"changestatus(this,".$task['t_id'].")\" type=\"checkbox\" value=\"1\"  checked=\"checked\"/> ";
					  
					  }else{
						$str.="<input id=\"task_done\" name=\"task[done]\" onclick=\"changestatus(this,".$task['t_id'].")\" type=\"checkbox\" value=\"1\"  /> ";
					  }
							
					$str.="  </div> 
						  <div id='_".$task['t_id']."' class=\"body\"> 
							<span class=\"hover_target\"> ";
						switch($task['t_rank'])
						{
							case 5:
								$str.="[<font>每日</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 4:
								$str.="[<font>BUG</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 3:
								$str.="[<font color=red>紧急</font>]<font color=red>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
							case 2:
								$str.="[<font color=#CC6633>比较</font>]"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name'];break;
							case 1:
								$str.="<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name'];break;
							default :
								$str.="[<font>常用网址</font>]<font>"."<a href='".base_url()."index.php/task/gettotask/".$task['ut_id']."'>".$task_title."</a> ".$task['t_name']."</font>";break;
						}
						
					 $str.="    <font color=\"#999999\" size=\"-1\">(".$task['completedate'].")</font><span class=\"appendTask\" style=\"color:#999999; font-size:12px; margin-left:10px\"></span>
							</span> 
							<span id=\"\" class=\"FR folder pointer\"><img src=\"".base_url()."images/022.png\" /></span>
						  </div> 
						  <div class=\"clear\"></div>
						</div>
						<div class=\"task_desc_list\" style=\"display:none;padding-left:30px;\">".$str_desc."</div>
						<div  class=\"task_c\"  style=\"height:0px;\" ><textarea style=\"display:none;\" id=\"task_text_".$task['t_id']."\" name=\"".$task['t_id']."\" ></textarea></div>
					  </div> 
					</li>";
				}
			}
		}
		//echo $str;
		//json->decode();
		
		$result['content']=$str;
		die(json_encode($result));
	}
	//ajax改变人物完成与否的状态
	/**
	*$b 选择表 5用每日表
	*/
	public function changestatus($t_id,$status,$tab)
	{
		$this->tk->changestatus($t_id,$status,$tab);
	}
	//增加人物描述
	public function addtms_desc()
	{	
		$contents=$this->input->post('content');
		$t_id=$this->input->post('t_id');
		$contents=urldecode($contents);
		$this->tk->addtms_desc($t_id,$contents);
		$desc_list=$this->tk->getall_desc($t_id);
		$str='';
		$i=1;
		foreach($desc_list as $desc)
		{
			$str.=$i.".".$desc['d_content']."<br>";
			$i++;
		}
		$result['content']=$str;
		die(json_encode($result));
	}
	public function checklogining()
	{
		if(!($this->session->userdata('u_id')))
		{
			header("location:".site_url('user/index'));
		}
	
	}
	public function getdonetask()
	{
		$this->checklogining();
		$uid=$this->session->userdata('u_id');
		if($this->session->userdata('rank')>0)
		{
			$user_list=$this->tk->getalluser();
			$data["user_list"]=$user_list;
			$taskdonelist=$this->tk->getdonealltask();//print_r($tasktolist);
			foreach($taskdonelist as $key=>$task)
			{
				
				$desc_list=$this->tk->getall_desc($task['t_id']);
				$str='';
				$i=1;
				foreach($desc_list as $desc)
				{
					$str.=$i.".".$desc['d_content']."<br>";
					$i++;
				}
				$taskdonelist[$key]['desc']=$str;
			}
			
		}else{
			$taskdonelist=$this->tk->getdonetask($uid);
			foreach($taskdonelist as $key=>$task)
			{
				
				$desc_list=$this->tk->getall_desc($task['t_id']);
				$str='';
				$i=1;
				foreach($desc_list as $desc)
				{
					$str.=$i.".".$desc['d_content']."<br>";
					$i++;
				}
				$taskdonelist[$key]['desc']=$str;
			}
		}
		$data["task_list"]=$taskdonelist;
		//print_r($data);
		$this->load->view('donetask',$data);
	}
	//工作职责
	public function duty($uid)
	{
		$this->checkadmin();
		$dutyobj=$this->tk->getduty($uid);
		$data["uid"]=$dutyobj->uid;
		$data["duty"]=$dutyobj->duty;//print_r($data);die();
		$user_list=$this->tk->getalluser();
		$data["user_list"]=$user_list;
		$this->load->view('duty',$data);
	}
	//查看个人职责
	public function changeduty()
	{
		$uid=$this->input->post('uid');
		$dutyobj=$this->tk->getduty($uid);
		$user_list=$this->tk->getalluser();

		$content=$dutyobj->duty;
		$str="	<input type=\"hidden\" name=\"uid\" value=\"".$uid."\" /><script type=\"text/javascript\">
		<textarea cols=\"4\" rows=\"5\" name=\"content\">".$content."</textarea>";
		$result['content']=$str;
		die(json_encode($result));
	}
	//处理提交的职责
	public function subduty()
	{
		$uid=$this->input->post('uid');
		$content=addslashes($this->input->post('content'));
		$result=$this->tk->modifyduty($uid,$content);
		$url="task/duty/".$uid;
		if($result)
		{
			echo "<script>alert('提交成功!');window.location.href='".site_url($url)."'</script>";
		}else{
			echo "<script>alert('修改失败!');window.location.href='".site_url($url)."'</script>";
		}
	}
	public function checkadmin()
	{
		if(!($this->session->userdata('rank')>0))
		{		
			header("location:".site_url("task/gettotask"));
		}
	}
	
	public function search()
	{
		$users = $this->input->post('user');
		
		$data['uid']=$this->session->userdata('u_id');
		$user_list=$this->tk->getalluser();
		$data["user_list"]=$user_list;
		
		$data['yiwancheng'] = $this->search->wanchenListByuidWithstatus($users,1);
		$data['weiwancheng'] = $this->search->wanchenListByuidWithstatus($users,0);

		$this->load->view('resultSearch',$data);
	}
	
	//用ajax提示任务数
	public function tasknum()
	{
		$user_id = $this->session->userdata('u_id');
		$r = $this->tk->gettotask_count($user_id,1);
		$result['tasknum'] = $r['c'];
		die(json_encode($result));
	}
}
//array("name"=>"a","b","c");

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>